package com.bocloud.cmp.dao.app.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.app.ApplicationSoftwareInfoDao;
import com.bocloud.cmp.entity.app.resource.ApplicationSoftwareInfo;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 
 * @author zjm
 * @date 2017年3月18日
 */
@Repository
public class ApplicationSoftwareInfoDaoImpl extends BasicDao implements ApplicationSoftwareInfoDao {

	@Override
	public boolean saveApplicationSoftwareInfo(ApplicationSoftwareInfo applicationSoftwareInfo) throws Exception {
		return this.baseSaveEntity(applicationSoftwareInfo);
	}

	@Override
	public boolean deleteApplicationSoftwareInfo(ApplicationSoftwareInfo applicationSoftwareInfo) throws Exception {
		return this.baseDelete(applicationSoftwareInfo);
	}

	@Override
	public ApplicationSoftwareInfo getByAppIdAndSoftwareId(Long appId, Long softwareId) throws Exception {
		String sql = "select * from application_software_info where "
				+ "application_id = :appId and software_id = :softwareId ";
		Map<String, Object> params = MapTools.simpleMap("appId", appId);
		params.put("softwareId", softwareId);
		List<Object> list = this.queryForList(sql, params, ApplicationSoftwareInfo.class);
		List<ApplicationSoftwareInfo> result = new ArrayList<>();
		for (Object object : list) {
			result.add((ApplicationSoftwareInfo) object);
		}
		if (list.isEmpty()) {
			return null;
		}
		return result.get(0);
	}

	@Override
	public boolean updateApplicationSoftwareInfo(ApplicationSoftwareInfo applicationSoftwareInfo) throws Exception {
		return updateEntity(applicationSoftwareInfo);
	}

	@Override
	public List<ApplicationSoftwareInfo> getByAppId(Long appId) throws Exception {
		String sql = "select * from application_software_info where " + "application_id = :appId";
		Map<String, Object> params = MapTools.simpleMap("appId", appId);
		List<Object> list = this.queryForList(sql, params, ApplicationSoftwareInfo.class);
		List<ApplicationSoftwareInfo> applicationSoftwareInfos = new ArrayList<ApplicationSoftwareInfo>();
		for (Object object : list) {
			applicationSoftwareInfos.add((ApplicationSoftwareInfo) object);
		}
		if (list.isEmpty()) {
			return null;
		}
		return applicationSoftwareInfos;
	}

	@Override
	public ApplicationSoftwareInfo get(Long id) throws Exception {
		String sql = "select * from application_software_info where " + "id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Object> list = this.queryForList(sql, params, ApplicationSoftwareInfo.class);
		List<ApplicationSoftwareInfo> result = new ArrayList<>();
		for (Object object : list) {
			result.add((ApplicationSoftwareInfo) object);
		}
		if (list.isEmpty()) {
			return null;
		}
		return result.get(0);
	}

	@Override
	public List<ApplicationSoftwareInfo> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select asi.*, s.version as version, mu.`name` as mender_name, "
				+ "s.`status` as status, s.gmt_modify as gmt_modify "
				+ "from application_software_info asi "
				+ "LEFT join software s on s.id = asi.software_id "
				+ "LEFT JOIN `user` mu on mu.id = s.mender_id  where 1=1 ";
		sql = SQLHelper.buildSql(sql, page, rows, params, sorter, "");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<Object> list = this.queryForList(sql, paramMap, ApplicationSoftwareInfo.class);
		List<ApplicationSoftwareInfo> applicationSoftwareInfos = new ArrayList<>();
		ApplicationSoftwareInfo applicationSoftwareInfo = null;
		for (Object obj : list) {
			applicationSoftwareInfo = (ApplicationSoftwareInfo) obj;
			applicationSoftwareInfos.add(applicationSoftwareInfo);
		}
		return applicationSoftwareInfos;
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(*) from application_software_info asi "
				+ "LEFT join software s on s.id = asi.software_id "
				+ "LEFT JOIN `user` mu on mu.id = s.mender_id  where 1=1 ";
		sql = SQLHelper.buildSql(sql, params, null, "");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}
}
